package com.hui.dead;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class DeadRabbitConfig {
    // 死信的交换机名
    final static String DEAD_LETTER_EXCHANGE = "dead_exchange";
    final static String DEAD_ROUTING_KEY = "dead_fail";
    final static String DEAD_ROUTING = "dead_queue";
    final static String MAIL_ROUTING = "mail_queue";

    //创建死信交换机
    @Bean
    public DirectExchange deadLetterExchange() {
        return new DirectExchange(DEAD_LETTER_EXCHANGE, true, false);
    }

    //    创建死信队列
    @Bean
    public Queue deadLetterQueue() {
        return new Queue(DEAD_ROUTING_KEY);
//        Map<String, Object> map = new HashMap<>();
//        map.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE);//设置死信交换机
//        map.put("x-dead-letter-routing-key", DEAD_ROUTING_KEY);//设置死信routingKey
//        return new Queue(DEAD_ROUTING, true, false, false, map);
    }

    //    绑定死信队列和死信交换机
    @Bean
    public Binding deadLetterBinding() {
        return BindingBuilder.bind(deadLetterQueue()).to(deadLetterExchange()).with(DEAD_ROUTING_KEY);
    }

    //    创建业务队列
    @Bean
    public Queue mailQueue() {
        Map<String, Object> map = new HashMap<>();
        map.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE);//设置死信交换机
        map.put("x-dead-letter-routing-key", DEAD_ROUTING_KEY);//设置死信routingKey
        return new Queue(MAIL_ROUTING, true, false, false, map);
    }

    //    绑定业务队列和交换机，指定routingKey
    @Bean
    public Binding mailBinding() {
        return BindingBuilder.bind(mailQueue()).to(DirectExchange.DEFAULT).with(MAIL_ROUTING);
    }

}
